﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace Uranus.Common
{
    public class ActionCommon
    {
        private static Queue<Func<object>> queue = new Queue<Func<object>>();
        private static ActionCommon actionCommon;
        private static object Obj = new object();
        private ActionCommon()
        {
            ThreadPool.QueueUserWorkItem(a =>
            {
                while (true)
                {
                    if (queue.Count > 0)
                    {
                        Func<object> action = queue.Dequeue();
                        if (action != null)
                        {
                            action();
                        }
                    }
                    else
                    {
                        Thread.Sleep(2000);
                    }
                }
            });
        }

        static Object obj = new Object();

        /// <summary>
        /// 加入操作
        /// </summary>
        /// <param name="action"></param>
        public static void AddAction(Func<object> action)
        {
            lock (obj)
            {
                if (actionCommon == null)
                {
                    actionCommon = new ActionCommon();
                }
                queue.Enqueue(action);
            }
        }
    }
}